/**
 * 页面加载完毕执行
 */
var contextPath;
$(function(){
	var parentFrame = window.parent;
	if(parentFrame && parentFrame != window){
		window.top.location.href = contextPath + "login.jsp";
	}
	//数据加载
	goToPage(companyId,userId,1);
});

function goToPage(companyId,userId,pagenum){
	$(".pwsj_right_xxzx_divbox").empty();
	$(".page").empty();
	$.ajax({
		url : contextPath+ 'user/companymessage/list',
		data : {
			pageszie : 10,
			pagenum : pagenum,
			companyId :companyId,
			userId : userId
		},
		type : 'POST',
		success : function(list) {
			//alert(list.records.length);
			var obj = "";
			if(list.totalRecord>0){
				for(var i=0;i<list.records.length;i++){
					var companymessage = list.records[i];
					obj = obj + "<ul class='pwsj_xxzx_cont_ul'><li class='checkbox_li' style='width:8%;'>" +
							"<input class='checkbox' type='checkbox' id='ckid' name='chkItem' " +"value='"+companymessage.messageId+"'/></li>";
					if(companymessage.messageType==1){
						obj = obj + "<li style='width:10%;'>通知</li>";
					}else if(companymessage.messageType==2){
						obj = obj + "<li style='width:10%;'>验证</li>";
					}
					obj = obj + "<li style='width:30%;'>";
					if(companymessage.content.length > 20){
						obj = obj + companymessage.content.substring(0,20);
					}else{
						obj = obj + companymessage.content;
					}
					obj = obj + "</li><li style='width:15%;'>"+ companymessage.createTimeStr +"</li>";
					obj = obj + "<li style='width:10%;'>";
					if(companymessage.messageType==2){
						//alert(companymessage.readStatus);
						if(companymessage.readStatus==1){
							obj = obj + "未读";
						}else if(companymessage.readStatus==2){
							obj = obj + "已读";
						}
					}
					obj = obj + "</li><li class='operate' style='width:20%; border-right:none;'>";
					if(companymessage.messageType==1){
						obj = obj + "<a href='readmessage.jsp?id="+companymessage.messageId+"' class='detail_a'>查看详细</a>";
					}else if(companymessage.messageType==2){
						if(companymessage.result == "1"){
							obj = obj +"<input class='agree' type='button' value='同意' onclick='javascript:ok("+companymessage.auditId+","+companymessage.messageId+");'/>" +
									"<input class='reject' type='button' value='拒绝' onclick='javascript:refuse("+companymessage.auditId+","+companymessage.messageId+");'/>";
						}else if(companymessage.result == "2"){
							obj = obj + "通过";
						}else{
							obj = obj + "已拒绝";
						}
					}
					obj = obj + "</li><div class='clear'></div></ul>";
					//alert(obj);
				}
			}else{
				obj = obj + "<ul class='pwsj_xxzx_cont_ul'><li class='checkbox_li' style='width:100%; text-algin:center;'>当前没有任何消息</li><div class='clear'></div></ul>";
			}
			$(".pwsj_right_xxzx_divbox").append(obj);
			//分页
			var fullListSize = list.totalRecord;
		 	var pageNumber = list.currPage;
		 	var totalPage=0;
		 	if (fullListSize % 10 == 0&&fullListSize!=0) {
				totalPage = fullListSize / 10;
			} else {
				totalPage = Math.floor(fullListSize / 10 + 1);
			}
		 	//alert(fullListSize);
		 	//alert(pageNumber);
		 	//alert(totalPage);
		 	if(fullListSize!=undefined && fullListSize!=null && fullListSize>=0){
		 		var start = 1;
	 			var end = totalPage;
	 			if(totalPage > 10 && pageNumber != 1) {
	 				if((totalPage-pageNumber)<8){
	 					start = pageNumber - (10 -(totalPage-pageNumber) -1);
	 				}else{
	 					start =  pageNumber -1;
	 				}
	 			} 
	 			if((totalPage-8)>pageNumber){
	 				if(pageNumber == 1) {
	 					end = 10;
	 				} else {
		 				end = pageNumber + 8;
	 				}
	 			}
	 			var prePage = pageNumber - 1 < 1 ? 1 : pageNumber - 1;
	 			var nextPage = pageNumber + 1 > totalPage ? totalPage : pageNumber + 1;
	 			
	 			$(".page").append('<a href="javascript:goToPage('+companyId+','+userId+',1)">首页</a> ');
	 			$(".page").append('<a href="javascript:goToPage('+companyId+','+userId+','+prePage+')">上一页</a> ');
	 			for(var i=start;i<=end;i++){
		 			if(i == pageNumber){
		 				if(i < 10) {
		 					$str = $('<a href="javascript:goToPage('+companyId+','+userId+','+i+')">'+i+'</a> ');
		 				} else {
		 					$str = $('<a href="javascript:goToPage('+companyId+','+userId+','+i+')">'+i+'</a> ');
		 				}
			 		}else{
			 			if(i < 10) {
			 				$str = $('<a href="javascript:goToPage('+companyId+','+userId+','+i+')">'+i+'</a> ');
			 			} else {
			 				$str = $('<a href="javascript:goToPage('+companyId+','+userId+','+i+')">'+i+'</a> ');
			 			}
			 		}
	 				$(".page").append($str);
				}
	 			$(".page").append(' <a href="javascript:goToPage('+companyId+','+userId+','+nextPage+')">下一页</a> ');
 				$(".page").append('<a href="javascript:goToPage('+companyId+','+userId+','+totalPage+')">末页</a>');
		 	}
		}
	});
}

function refuse(id,messageid){
	if(confirm("是否拒绝")){
		$.ajax({
			url : contextPath+ 'user/companymessage/refuse',
			data : {
				auditId : id,
				messageId : messageid
			},
			type : 'POST',
			success : function(data) {
				alert(data.msgDesc);
				goToPage(companyId,userId,1);
			}
		});
	}
}

function del(){
	var str="";
	$("input[name='chkItem']:checkbox").each(function(){ 
        if($(this).attr("checked")){
            str += $(this).val()+",";
        }
    });
	if(str==""){
		alert("至少选择一条数据");
		return false;
	}
	if(confirm("确定删除吗")){
		$.ajax({
			url : contextPath+ 'user/companymessage/del',
			data : {
				messageId : str
			},
			type : 'POST',
			success : function(data) {
				alert(data.msgDesc);
				goToPage(companyId,userId,1);
			}
		});
	}
}

function ok(id,messageid){
	if(confirm("确定通过吗")){
		$.ajax({
			url : contextPath+ 'user/companymessage/examine',
			data : {
				auditId : id,
				messageId : messageid
			},
			type : 'POST',
			success : function(data) {
				alert(data.msgDesc);
				goToPage(companyId,userId,1);
			},
			error : function(){
				alert('出错了');
			}
		});
	}
}